﻿using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.DTOs.ReportDTOs;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.Reports
{
    public class WhiteBarDatileHander : IRequestHandler<WhiteBarDetailsCommand, Resqults<WhiteBarDatileDTO>>
    {
        private readonly IBateRepository<WhiteNotes> white;
        private readonly IBateRepository<Shopping> shop;
        private readonly IBateRepository<Deductions> deductions;
        private readonly IBateRepository<Stash> stash;

        public WhiteBarDatileHander(IBateRepository<WhiteNotes> white, IBateRepository<Shopping> shop, IBateRepository<Deductions> deductions, IBateRepository<Stash> stash)
        {
            this.white = white;
            this.shop = shop;
            this.deductions = deductions;
            this.stash = stash;
        }

        public async Task<Resqults<WhiteBarDatileDTO>> Handle(WhiteBarDetailsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<WhiteBarDatileDTO> res = new Resqults<WhiteBarDatileDTO>();
                var list = (from a in white.GetAll()
                            join b in shop.GetAll() on a.ShoppingDesignationid equals b.ShoppingDesignationid
                            join c in deductions.GetAll() on a.Ductionid equals c.Ductionid
                            join d in stash.GetAll() on a.Stashid equals d.Stashid
                            select new WhiteBarDatileDTO()
                            {
                                StashName = d.StashName,
                                WhiteNoteTime = a.WhiteNoteTime,
                                ShoppingDesignationid = b.ShoppingDesignationName,
                                WhiteNoteNum = a.WhiteNoteNum,
                                WhiteNoteWeight = a.WhiteNoteWeight,
                                WhiteNoteTare = a.WhiteNoteTare,
                                NetWeight = a.NetWeight,
                                ScratchTheFat = a.ScratchTheFat,
                                Ductionid = c.DuctionName,
                                WhiteNoteRemark = a.WhiteNoteRemark,
                                SlaughterNumber = a.SlaughterNumber
                            }).ToList();
                if (request.BeginTime != null)
                {
                    list = list.Where(x=>x.WhiteNoteTime>= request.BeginTime).ToList();
                }
                if (request.EndTime != null)
                {
                    list = list.Where(x => x.WhiteNoteTime <= request.EndTime).ToList();
                }
                res.Date = list;
                return res;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
